package csu.web.mypetstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import csu.web.mypetstore.mapper.InventoryMapper;
import csu.web.mypetstore.pojo.Inventory;
import csu.web.mypetstore.pojo.Item;
import csu.web.mypetstore.pojo.OrderDetail;
import csu.web.mypetstore.pojo.Product;
import csu.web.mypetstore.pojo.Vo.GetItemByProductIdVo;
import csu.web.mypetstore.service.ItemService;
import csu.web.mypetstore.mapper.ItemMapper;
import csu.web.mypetstore.service.OrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 18632
 * @description 针对表【item】的数据库操作Service实现
 * @createDate 2025-03-10 16:55:17
 */
@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item>
        implements ItemService{

    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private InventoryMapper inventoryMapper;
    @Autowired
    private OrderDetailService orderDetailService;



    @Override
    public List<Item> getItemListByProduct(String productId) {

        List<Item> itemList=new ArrayList<>();

        List<GetItemByProductIdVo>getItemByProductIdVos=itemMapper.getItemListByProduct(productId);

        for(GetItemByProductIdVo getItemByProductIdVo:getItemByProductIdVos){

            itemList.add(setItemProduct(getItemByProductIdVo,itemMapper.selectOne(new LambdaQueryWrapper<Item>().eq(Item::getItemid,getItemByProductIdVo.getItemId())).getId()));
        }



        return itemList;
    }

    @Override
    public Item getItem(String itemId) {
        GetItemByProductIdVo getItemByProductIdVo =itemMapper.getItemById(itemId);
        int id=itemMapper.selectOne(new LambdaQueryWrapper<Item>()
                .eq(Item::getItemid,itemId)).getId();
        return setItemProduct(getItemByProductIdVo, id);
    }

    @Override
    public boolean isItemInStock(String itemId) {

        Wrapper<Inventory>wrapper=new LambdaQueryWrapper<Inventory>()
                .eq(Inventory::getItemid,itemId);
        Inventory inventory = inventoryMapper.selectOne(wrapper);


        if (inventory == null) {
            return false;
        }
        if (inventory.getQty() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public double getItemListPrice(String itemId) {
        Item item=getItem(itemId);
        return item.getListprice();
    }

    @Override
    public int getLeftItemNum(String itemId) {
        Wrapper<Inventory>wrapper=new LambdaQueryWrapper<Inventory>()
                .eq(Inventory::getItemid,itemId);
        Inventory inventory = inventoryMapper.selectOne(wrapper);
        return inventory != null ? inventory.getQty() : 0;
    }

    @Override
    public List<Item> getAllItem() {
        return itemMapper.selectList(null);
    }

    @Override
    public List<Inventory> getAllInventory() {
        return inventoryMapper.selectList(null);
    }

    @Override
    public void increaseItemNum(int num, String itemId) {
        Wrapper<Inventory>wrapper=new LambdaQueryWrapper<Inventory>()
                .eq(Inventory::getItemid,itemId);
        Inventory inventory = inventoryMapper.selectOne(wrapper);
        if (inventory != null) {
            inventory.setQty(inventory.getQty() + num);
            Wrapper<Inventory>wrapper1=new LambdaQueryWrapper<Inventory>()
                    .eq(Inventory::getItemid,itemId);
            inventoryMapper.update(inventory,wrapper1);
        }
    }

    @Override
    public void decreaseItemNum(int num, String itemId) {
        Wrapper<Inventory>wrapper=new LambdaQueryWrapper<Inventory>()
                .eq(Inventory::getItemid,itemId);
        Inventory inventory = inventoryMapper.selectOne(wrapper);
        if (inventory != null && inventory.getQty() >= num) {
            inventory.setQty(inventory.getQty() - num);
            Wrapper<Inventory>wrapper1=new LambdaQueryWrapper<Inventory>()
                    .eq(Inventory::getItemid,itemId);
            inventoryMapper.update(inventory,wrapper1);
        }
    }

    @Override
    public void removeItem(String itemId) {
        Wrapper<Item>wrapper=new LambdaQueryWrapper<Item>()
                .eq(Item::getItemid,itemId);
        itemMapper.delete(wrapper);
        Wrapper<Inventory>wrapper1=new LambdaQueryWrapper<Inventory>()
                .eq(Inventory::getItemid,itemId);
        inventoryMapper.delete(wrapper1);
    }

    @Override
    public void updateInventory(String itemId,String newId, int quantity) {
        Wrapper<Inventory>wrapper=new LambdaQueryWrapper<Inventory>()
                .eq(Inventory::getItemid,itemId);
        Inventory inventory = inventoryMapper.selectOne(wrapper);
        inventory.setItemid(newId);
        inventory.setQty(quantity);
        inventoryMapper.update(inventory,wrapper);
    }

    @Override
    public void moveItemToProduct(String itemId, String productId) {
        Item item=getItem(itemId);
        if (item != null) {
            item.setProductid(productId);
            itemMapper.updateById(item);
        }
    }

    @Override
    public void deleteItemInProduct(String productId) {
        Wrapper<Item>wrapper=new LambdaQueryWrapper<Item>()
                .eq(Item::getProductid,productId);
        itemMapper.delete(wrapper);
    }

    @Override
    public void insertItem(Item item,Integer qty) {
        itemMapper.insert(item);

       Inventory inventory=new Inventory();
       inventory.setItemid(item.getItemid());
       inventory.setQty(qty);
       inventoryMapper.insert(inventory);


    }

    @Override
    public void updateItem(Item item) {
        Wrapper<Item>wrapper=new LambdaQueryWrapper<Item>()
                .eq(Item::getId,item.getId());
        itemMapper.update(item,wrapper);

    }

    @Override
    public void decreaseItemInventoryInOrder(int oderId) {
        List<OrderDetail>orderDetailList=orderDetailService.queryOrderDetail(oderId);

        for(OrderDetail orderDetail:orderDetailList){
            decreaseItemNum(orderDetail.getQuantity(),orderDetail.getItemid());
        }
    }


    Item setItemProduct(GetItemByProductIdVo getItemByProductIdVo,int id)
    {

            String itemId = getItemByProductIdVo.getItemId();
            Item item=new Item();
            item.setId(id);
            item.setItemid(itemId);
            item.setListprice(getItemByProductIdVo.getListPrice().doubleValue());
            item.setUnitcost(getItemByProductIdVo.getUnitCost().intValueExact());
            item.setSupplier(getItemByProductIdVo.getSupplierId());
            item.setStatus(getItemByProductIdVo.getStatus());
            item.setAttr1(getItemByProductIdVo.getAttribute1());
            item.setAttr2(getItemByProductIdVo.getAttribute2());
            item.setAttr3(getItemByProductIdVo.getAttribute3());
            item.setAttr4(getItemByProductIdVo.getAttribute4());
            item.setAttr5(getItemByProductIdVo.getAttribute5());
            item.setQuantity(getLeftItemNum(itemId));

            Product product=new Product();
            product.setProductid(getItemByProductIdVo.getProductId());
            product.setName(getItemByProductIdVo.getName());
            product.setCategory(getItemByProductIdVo.getCategoryId());
            product.setDescn(getItemByProductIdVo.getDescription());

            item.setProduct(product);
            return  item;

    }
}




